package zjhr.com.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zjhr.com.mapper.TUserMapper;
import zjhr.com.pojo.TUser;
import zjhr.com.pojo.TUserExample;
import zjhr.com.pojo.TUserExample.Criteria;
import zjhr.com.service.LoginService;
import zjhr.com.utils.TaotaoResult;
@Service
public class LoginServiceImpl implements LoginService {
	@Autowired
	private TUserMapper userMapper;
	@Override
	public TUser findUserByUserNamePassword(String username, String password) {
		if(StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){
			TUserExample example = new TUserExample();
			Criteria criteria = example.createCriteria();
			criteria.andUsernameEqualTo(username);
			criteria.andPasswordEqualTo(password);
			List<TUser> list = userMapper.selectByExample(example);
			if(list !=null &&list.size()>0){
				return list.get(0);
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	//修改密码
	@Override
	public TaotaoResult updatePasswordById(String id, String password) {
		if(StringUtils.isNotBlank(id) && StringUtils.isNotBlank(password)){
			TUserExample example = new TUserExample();
			Criteria criteria = example.createCriteria();
			criteria.andIdEqualTo(id);
			TUser user = userMapper.selectByPrimaryKey(id);
			user.setPassword(password);
			userMapper.updateByExample(user, example);
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.build(300, "Sourry,您的操作有误");
		}
		
	}

}
